/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.sharding.sample.singledb.multitable.service.impl;

import com.lvyh.lightframe.sharding.client.annotations.IdGenInfo;
import com.lvyh.lightframe.sharding.client.annotations.ShardingInfo;
import com.lvyh.lightframe.sharding.client.common.Constants;
import com.lvyh.lightframe.sharding.client.pagination.DefaultPageResult;
import com.lvyh.lightframe.sharding.client.pagination.PageParam;
import com.lvyh.lightframe.sharding.sample.singledb.multitable.bean.TradeUserBean;
import com.lvyh.lightframe.sharding.sample.singledb.multitable.bean.TradeUserParamInfo;
import com.lvyh.lightframe.sharding.sample.singledb.multitable.service.ShardingMainDataService;
import com.lvyh.lightframe.sharding.sample.singledb.multitable.service.TradeUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@ShardingInfo(logicTable = "trade_user", tableShardNum = 2, shardAlg = Constants.TYPE_HASH)
@Service
public class TradeUserServiceImpl implements TradeUserService {

    @Autowired
    private ShardingMainDataService shardingMainDataService;

    @Override
    @IdGenInfo(idGenType = Constants.GEN_TYPE, idGenTag = Constants.PAY, column = "id")
    public void insert(TradeUserBean tradeUserBean) throws Exception {
        shardingMainDataService.saveTradeUser(tradeUserBean);
    }

    @Override
    public boolean update(TradeUserBean tradeUserBean) throws Exception {
        TradeUserBean updateBean = shardingMainDataService.selectTradeUserById(tradeUserBean.getId());
        if (updateBean == null) {
            return false;
        }
        BeanUtils.copyProperties(tradeUserBean, updateBean);
        return shardingMainDataService.updateTradeUser(updateBean);
    }

    @Override
    public boolean deleteUser(TradeUserBean tradeUserBean) throws Exception {
        return shardingMainDataService.deleteTradeUser(tradeUserBean.getId());
    }

    @Override
    public TradeUserBean selectById(TradeUserBean tradeUserBean) throws Exception {
        return shardingMainDataService.selectTradeUserById(tradeUserBean.getId());
    }

    @Override
    public List<TradeUserBean> selectList(TradeUserParamInfo param) throws Exception {
        TradeUserBean condition = new TradeUserBean();
        BeanUtils.copyProperties(param, condition);

        List<TradeUserBean> result = shardingMainDataService.selectTradeUser(condition);
        return result;
    }

    @Override
    public List<TradeUserBean> selectListByPage(TradeUserParamInfo param) throws Exception {
        TradeUserBean condition = new TradeUserBean();
        BeanUtils.copyProperties(param, condition);

        PageParam pageParam = new PageParam();
        pageParam.setPageNo(param.getPageNum());
        pageParam.setPageSize(param.getPageSize());
        pageParam.setOrderColumns("update_time desc");
        DefaultPageResult<TradeUserBean> pageInfo = shardingMainDataService.selectTradeUserByCondition(condition, pageParam);

        if (pageInfo == null || CollectionUtils.isEmpty(pageInfo.getResultList())) {
            return null;
        }

        List<TradeUserBean> result = pageInfo.getResultList();
        return result;
    }

}
